package com.heng.groupchat.controller;

import com.heng.groupchat.common.BaseResponse;
import com.heng.groupchat.common.ResultUtils;
import com.heng.groupchat.common.StatusCode;
import com.heng.groupchat.exception.BusinessException;
import com.heng.groupchat.model.dto.UserLoginRequest;
import com.heng.groupchat.model.dto.UserRegisterRequest;
import com.heng.groupchat.model.entity.User;
import com.heng.groupchat.model.vo.UserVO;
import com.heng.groupchat.service.UserService;
import com.heng.groupchat.websocket.WebSocket;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户接口
 *
 * @author faye
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Resource
    private UserService userService;

    @Resource
    private WebSocket webSocket;

    /**
     * 用户注册
     *
     * @param userRegisterRequest 用户注册请求
     * @return 响应体
     */
    @PostMapping("/register")
    public BaseResponse<Long> userRegister(
        @NotNull @Valid @RequestBody UserRegisterRequest userRegisterRequest
    ) {
        //调用业务层
        String userAccount = userRegisterRequest.getUsername();
        String userPassword = userRegisterRequest.getPassword();
        String checkPassword = userRegisterRequest.getCheckPassword();
        long result = userService.userRegister(userAccount, userPassword, checkPassword);
        return ResultUtils.success(result);
    }

    /**
     * 检查账号可用性，即账号是否已存在
     * @param username 账号
     * @return 是否可用
     */
    @GetMapping("/check/username")
    public BaseResponse<Boolean> checkUsernameValid(String username) {
        if(username == null) {
            return ResultUtils.success(false);
        }
        User user = userService.getUserByUsername(username);
        return ResultUtils.success(user == null);
    }

    /**
     * 用户登录
     *
     * @param userLoginRequest 用户登录请求体
     * @return 用户信息响应体
     */
    @PostMapping("/login")
    public BaseResponse<UserVO> userLogin(
        @NotNull @Valid @RequestBody UserLoginRequest userLoginRequest,
        HttpServletRequest request
    ) {
        String username = userLoginRequest.getUsername();
        String password = userLoginRequest.getPassword();
        User user = userService.getUserByUsername(username);
        if(!user.getPassword().equals(password)) {
            throw new BusinessException(StatusCode.LOGIN_ERROR, "密码错误");
        }
        request.getSession().setAttribute("USER_LOGIN_STATE", user);
        // 返回用户视图
        UserVO userVO = UserVO.objToVO(user);
        return ResultUtils.success(userVO);
    }

    /**
     * 获取当前用户
     * @return 用户信息响应体
     */
    @GetMapping("/current")
    public BaseResponse<UserVO> getCurrentUser(HttpServletRequest request) {
        User user = userService.getCurrentUser(request);
        UserVO userVO = UserVO.objToVO(user);
        return ResultUtils.success(userVO);
    }

    /**
     * 获取所有在线用户
     * @return 用户信息响应体
     */
    @GetMapping("/all")
    public BaseResponse<List<String>> getAllUser() {
        List<String> users = webSocket.getAllUser();
        return ResultUtils.success(users);
    }

    /**
     * 用户注销
     * @param request HTTP请求
     * @return 注销结果响应体
     */
    @GetMapping("/logout")
    public BaseResponse<Boolean> userLogout(
        HttpServletRequest request
    ) {
        request.getSession().removeAttribute("USER_LOGIN_STATE");
        return ResultUtils.success(true);
    }
}

